package com.bw.day03

import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.net.mvvm.BaseActivity
import com.bw.base.net.mvvm.BaseViewActivity
import com.bw.day03.change.ChangeActivity
import com.bw.day03.databinding.ActivityMain2Binding
import com.bw.day03.userinfo.UserViewModel
import com.bw.day03.util.GlideUtlis
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.OptionPicker
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

@Route(path = "/app/MainActivity2")
class MainActivity2 : BaseActivity<ActivityMain2Binding, UserViewModel>() {
    var imgPath=""
    override fun initView() {
        viewModel.user()
        binding.change.setOnClickListener {
            startActivity(Intent(this, ChangeActivity::class.java))
        }
        binding.img.setOnClickListener {
            PictureSelector.create(this,102).selectPicture()
        }
        binding.phone.setOnClickListener {
            val datePicker = DatePicker(this)
            datePicker.setTitle("请选择时间")
            datePicker.wheelLayout.setRange(DateEntity.target(1990, 1, 1), DateEntity.today(),
                DateEntity.today())
            datePicker.setOnDatePickedListener {year,  month, day ->
                ToastUtils.showLong("$year-$month-$day")
            }
            datePicker.show()
        }
        binding.uid.setOnClickListener {
            val listOf = mutableListOf("男", "女", "保密")
            val optionPicker = OptionPicker(this)
            optionPicker.setTitle("请选择性别")
            optionPicker.setData(listOf)
            optionPicker.setOnOptionPickedListener { position, item ->
                ToastUtils.showLong(listOf[position])
            }
            optionPicker.show()
        }
    }
    override fun initData() {
        viewModel.uploadSuccess.observe(this){
            if (it.code==200){
                imgPath=it.data.url
            }
        }
    }
    override fun getViewModel(): Class<UserViewModel> = UserViewModel::class.java

    override fun getLayoutId(): Int =  R.layout.activity_main2
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode==102&&resultCode==RESULT_OK){
            val bean = data?.extras?.get(PictureSelector.PICTURE_RESULT) as PictureBean
            GlideUtlis.loadCircle(this,bean.path,binding.img)
            val create = RequestBody.create("multipart/form-dat".toMediaType(), File(bean.path))
            val multipartBodyPart = MultipartBody.Part.createFormData("multipart", "aa.png", create)
            viewModel.upload("user",multipartBodyPart,"0")
        }
    }

}